package com.ruanyun.modular.admin.service.impl;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.consts.PublicCommon;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.modular.admin.entity.TMeasures;
import com.ruanyun.modular.admin.mapper.TCommitReportMapper;
import com.ruanyun.modular.admin.model.result.TMonitoringinfoResult;
import com.ruanyun.modular.admin.service.VocReducingPlanService;
import com.ruanyun.sys.core.shiro.ShiroKit;
import com.ruanyun.modular.admin.mapper.TMeasuresMapper;
import com.ruanyun.modular.admin.model.params.TMeasuresParam;
import com.ruanyun.modular.admin.model.result.TMeasuresResult;
import  com.ruanyun.modular.admin.service.TMeasuresService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.sys.modular.system.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 措施信息 服务实现类
 * </p>
 *
 * @author zs
 * @since 2021-05-11
 */
@Service
public class TMeasuresServiceImpl extends ServiceImpl<TMeasuresMapper, TMeasures> implements TMeasuresService {
    @Autowired
    private UserService userService;
    @Autowired
    private VocReducingPlanService vocReducingPlanService;
    @Autowired
    private TCommitReportMapper commitReportMapper;


    @Override
    public ResponseData add(TMeasuresParam param){


        TMeasures entity = getEntity(param);
        entity.setDelFlag(PublicCommon.DEL_FLAG_NORMAL);
        Integer count = commitReportMapper.getIsCommit(userService.getCid());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法继续添加");
        }

        int result = this.baseMapper.insert(entity);
        if(result == 0){
            return ResponseData.error("添加失败");
        }
        return ResponseData.success();
    }

    @Override
    public void delete(TMeasuresParam param){
        this.removeById(getKey(param));
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @Override
    public ResponseData updateDelFlagById(Long id) {
        TMeasures entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该措施信息不存在");
        }
        entity.setDelFlag(PublicCommon.DEL_FLAG_DELETE);
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("删除失败");
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(TMeasuresParam param){
        TMeasures oldEntity = getOldEntity(param);
        TMeasures newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        Integer count = commitReportMapper.getIsCommit(userService.getCid());
        if(count > 0 ){
            return  ResponseData.error("该条信息已提交报告无法修改");
        }
        int result = this.baseMapper.updateById(newEntity);
        if(result == 0){
        return ResponseData.error("修改失败失败");
        }
        return ResponseData.success();
    }

    @Override
    public TMeasuresResult findBySpec(TMeasuresParam param){
        return null;
    }

    @Override
    public List<TMeasuresResult> findListBySpec(TMeasuresParam param){
        return null;
    }



    private Serializable getKey(TMeasuresParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TMeasures getOldEntity(TMeasuresParam param) {
        return this.getById(getKey(param));
    }

    private TMeasures getEntity(TMeasuresParam param) {
        TMeasures entity = new TMeasures();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    /**
        * 启用
        *
        * @param id
        * @return
        */
    @Override
    public ResponseData enableById(Long id) {
        TMeasures entity = this.baseMapper.selectById(id);
        if (entity == null) {
        return ResponseData.error("该措施信息不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("启用失败");
        }
        return ResponseData.success();
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @Override
    public ResponseData disableById(Long id) {
        TMeasures entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return ResponseData.error("该措施信息不存在");
        }
        int result = this.baseMapper.updateById(entity);
        if (result == 0) {
            return ResponseData.error("禁用失败");
        }
        return ResponseData.success();
    }


    @Override
    public LayuiPageInfo findPageBySpec(Integer redId,Integer redType,Integer measureType){
        Page pageContext = getPageContext();
        TMeasuresParam param = new TMeasuresParam();
        param.setRedId(redId);
        param.setRedType(redType);
        param.setMeasureType(measureType);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }



    @Override
    public LayuiPageInfo measureInfo(Integer companyId) {
        Page pageContext = getPageContext();
        TMeasuresParam param = new TMeasuresParam();

          // 根据当前公司id 获取综合减排措施id
        Integer rid = vocReducingPlanService.getIdByCid(companyId);
         param.setRedId(rid);
        IPage page = this.baseMapper.customPageList1(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Page<TMeasuresResult> getMeasurePage(TMeasuresParam measuresParam) {
        Page pageContext = getPageContext();
        Page<TMeasuresResult>  results =  baseMapper.getMeasurePage(pageContext,measuresParam);
        return results;

    }

    @Override
    public List<TMeasuresResult> getMeasureList(Integer companyId) {
        Integer redId = vocReducingPlanService.getIdByCid(companyId);

        return baseMapper.getMeasureList(redId);
    }

    @Override
    public TMeasuresResult measureHeadInfo(Integer companyId) {
        // 根据当前公司id 获取综合减排措施id
        Integer rid = vocReducingPlanService.getIdByCid(companyId);

        return  this.baseMapper.measureHeadInfo(rid);
    }

}
